<?php
class Frontend_Model_Query extends Custom_Model_Abstract
{
    /**
     * Find query by their id
     *
     * @param int  $id
     * @return null|Wenda_Table_Row_Query
     */
    public function findById($id)
    {
        return Wenda::getTable('Query')->findById($id);
    }

    /**
     * Get recommand queries
     *
     * @param int $categoryId
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getRecommend($categoryId=null, $limit=10, $page=null)
    {
        return Wenda::getTable('Query')->getRecommend($categoryId,$limit,$page);
    }

    /**
     * Get Issued queries
     * 
     * @param int $categoryId
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getIssued($categoryId=null, $limit=10, $page=null)
    {
        return Wenda::getTable('Query')->getIssued($categoryId,$limit,$page);
    }

    /**
     * Get resolved queries
     *
     * @param int $categoryId
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getResolved($categoryId=null, $limit=10, $page=null)
    {
        return Wenda::getTable('Query')->getResolved($categoryId,$limit,$page);
    }

    /**
     * Get hight reward queries
     *
     * @param int $categoryId
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getHightReward($categoryId=null, $limit=10, $page=null)
    {
        return Wenda::getTable('Query')->getHightReward($categoryId,$limit,$page);
    }

    /**
     * Get no answer queries
     *
     * @param int $categoryId
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getNoAnswer($categoryId=null, $limit=10, $page=null)
    {
        return Wenda::getTable('Query')->getNoAnswer($categoryId,$limit,$page);
    }

    /**
     * Save query data to db
     *
     * @param array $data
     * @param string $validator
     * @return boolean
     */
    public function save($data , $validator= null)
    {
        $this->setDefault($data);

        if (null === $validator) {
            $validator = 'Submit';
        }

        $validator = Wenda::getForm('query' . ucfirst($validator));

        if (!$validator->isValid($data)) {
            return false;
        }

        if (array_key_exists('id',$data)) {
            $row = $this->findById($data['id']);
        } else {
            $row = null;
            $data['created_at'] = date('Y-m-d H:i:s', time());
        }

        if (Wenda::getTable('Query')->saveRow($data,$row)){
            return Wenda::getModel('Category')->updateScores($data['category_id']);
        }
        return false;
    }

    public function setDefault(&$data,$type='submit')
    {
        $request = new Zend_Controller_Request_Http();

        $auth = Wenda::getService('authentication')->getAuth();
        $user = $auth->hasIdentity() ? $auth->getStorage()->read() : array('id'=>1);
        
        $data['user_id']    = $user['id'];
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        $data['status']     = 'A';
        $data['ip_address'] = $request->getClientIp();
    }

    public function updatePageviews($id)
    {
        $row = $this->findById($id);
        $row->pageviews = $row->pageviews + 1;
        $row->save();
    }

    public function adoptAnswer($queryId,$answerId)
    {
        $query = $this->findById($queryId);
        if (null === $query) {
            return false;
        }

        $answer = Wenda::getModel('Answer')->findById($answerId);
        if (null === $answer) {
            return false;
        }

        $query->finish = 'Y';
        $query->resolved_at = date('Y-m-d H:i:s', time());
        $query->save();

        $answer->finale = 'Y';
        $answer->save();

        return true;
    }
}